package com.ht.file.service;

import cn.hutool.core.io.FileUtil;
import com.ht.common.core.utils.StringUtils;
import com.ht.common.core.utils.file.FileUtils;
import com.ht.file.domain.SysAttachFile;
import com.ht.file.utils.FileUploadUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Primary;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.util.Date;
import java.util.List;

/**
 * 本地文件存储
 *
 * @author ruoyi
 */
@Primary
@Service
public class LocalSysFileServiceImpl implements ISysFileService {
    /**
     * 资源映射路径 前缀
     */
    @Value("${file.prefix}")
    public String localFilePrefix;

    /**
     * 域名或本机访问地址
     */
    @Value("${file.domain}")
    public String domain;

    /**
     * 上传文件存储在本地的根路径
     */
    @Value("${file.path}")
    private String localFilePath;
    @Autowired
    private FileCheckService fileCheckService;

    @Autowired
    private ISysAttachFileService sysAttachFileService;

    /**
     * 本地文件上传接口
     *
     * @param file 上传的文件
     * @return 访问地址
     * @throws Exception
     */
    @Override
    public SysAttachFile uploadFile(MultipartFile file) throws Exception {



        fileCheckService.checkFileLegal(file);
        String name = FileUploadUtils.upload(localFilePath, file);
        String url = domain + localFilePrefix + name;
        SysAttachFile sysAttachFile = saveSysAttachFile(name, file);
        sysAttachFile.setUrl(url);
        return sysAttachFile;
    }

    @Override
    public SysAttachFile selectSysAttachFileByFilename(String filename) {
        return sysAttachFileService.selectSysAttachFileByFilename(filename);
    }

    private SysAttachFile saveSysAttachFile(String name, MultipartFile file) {
        SysAttachFile saf = new SysAttachFile();
        saf.setOriginName(file.getOriginalFilename());
        saf.setCreateTime(new Date());
        saf.setExtName(name.substring(name.lastIndexOf(".") + 1));
        saf.setFullPath(localFilePath + name);
        saf.setFilename(FileUtils.getName(name));
        String ymdDir = name.substring(1, name.lastIndexOf("/"));
        saf.setYmdDir(ymdDir);
        //KB
        Double fileSize = file.getSize() / 1024d;
        saf.setSize(fileSize.longValue());
        sysAttachFileService.insertSysAttachFile(saf);
        return saf;
    }

    public static void main(String[] args) {
        String name = "/2025/03/26/fa92cd96-a94a-46b0-bc6c-51cd94814a23.jpeg";
        String ymdDir = name.substring(1, name.lastIndexOf("/"));
        System.out.println(ymdDir);
    }

    @Override
    public void initData() {
        if (StringUtils.isBlank(localFilePath)) {
            return;
        }
        List<File> files = FileUtil.loopFiles(localFilePath);
        if (StringUtils.isEmpty(files)) {
            return;
        }
        for (File file : files) {
            if (file.isFile()) {
                SysAttachFile dbFile = sysAttachFileService.selectSysAttachFileByFilename(file.getName());
                if (null == dbFile) {
                    SysAttachFile saf = new SysAttachFile();
                    saf.setCreateTime(new Date());
                    saf.setExtName(FileUtil.extName(file.getName()));
                    saf.setFullPath(file.getPath());
                    saf.setFilename(file.getName());
                    String ymdDir = file.getPath().substring(localFilePath.length() + 1, file.getPath().length() - file.getName().length() - 1);
                    saf.setYmdDir(ymdDir);
                    sysAttachFileService.insertSysAttachFile(saf);
                }
            }
        }
    }
}
